import requests
from urllib.parse import quote

from bs4 import BeautifulSoup
import mysql.connector

def pa(p):

    #第几页
    print(p)
    #学校名称
    schoolName = "东北大学"

    #爬取链接
    url = "http://job.neu.edu.cn/campus/index?domain=jobneu&city=&page="+p

    #请求头
    headers = {

    }
    #爬取数据并解析
    html = requests.get(url=url, headers=headers)
    html.encoding = "utf-8"

    soup = BeautifulSoup(html.text, 'lxml')

    #找到所有<ul class="infoList">标签
    dataList = soup.find_all("ul",class_="infoList")

    for data in dataList:
        createTime = data.find("li",class_="span4").text
        jobTitle = data.find("li",class_="span7").text
        print(createTime+"|"+jobTitle)
        mysql_conn(jobTitle,schoolName,createTime)

#数据库连接
def mysql_conn(job_title,school_name,create_time):
    mydb = mysql.connector.connect(
        host="127.0.0.1",
        user="root",
        passwd="",
        database="hschool_job"
    )
    mycursor = mydb.cursor()

    #插入
    sql = "INSERT INTO h_job (job_title,school_name,create_time) VALUES (%s,%s,%s)"
    val = (job_title,school_name,create_time)
    mycursor.execute(sql, val)
    mydb.commit()

if __name__ == '__main__':
    for i in range(1,652):
        pa(str(i))
